import {HTTP} from "../../../../utils/http.js"
import Dialog from '../../../../miniprogram_npm/vant-weapp/dialog/dialog';
var http = new HTTP();
import {formatDate} from "../../../../utils/util.js"

var _self;
Page({
  data: {
    src: '',
    schoolLogo:'https://admin.51imu.com/file/resources/imulogo.jpg',
    schoolName:"内蒙古大学",
    degreeIndex:1,
    dateofEnrollment:'2010-09',
    dateofGraduation:'2014-06',
    degreeArr:["专科","本科","硕士","博士","博士后","其他"],
    instituteArr:[],
    instituteIndex:0,
    subjectArr:[],
    subjectIndex:0,
    classArr:[],
    classIndex:0,
    schoolColleageArr:[],
    summary:"",
    type:"",
    editData:null,
    placeholder:"请输入在校总结",
    btnName:"保存"
  },
  upload() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        wx.redirectTo({
          url: `./upload/upload?src=${src}`
        })
      }
    })
  },
  onLoad(options) {
    _self = this;
    if (options.type && options.type == "edit") {
      _self.setData({
        placeholder:"",
        btnName:"更新",
        type: options.type,
        editData: JSON.parse(options.current)
      })
      this.initEditData()
    }else{
      //默认入学年份 加载学院信息
      _self.initCollegeInfo();
    }
  },
  initCollegeInfo:function(callback){
    let date = this.data.dateofEnrollment;
    let updata = {
      year: date.substr(0, 4)
    }
    _self.getSchooInfo(updata, "instituteArr");
    // if(callback){
    //   callback();
    // }
  },
  initEditData:function(){
    let data = this.data.editData;
    if (data!=null){ 

      let startDate = formatDate(data.startDate);
        this.setData({
          schoolName: data.schoolName,
          summary: data.summary,
          degreeIndex: this.data.degreeArr.indexOf(data.eucationalBackground),
          dateofEnrollment:startDate
        })
      _self.initCollegeInfo();
        // _self.initCollegeInfo(function(){
        //   _self.setData({
        //     instituteIndex: _self.data.instituteArr.indexOf(data.college),
        //     subjectIndex: _self.data.subjectArr.indexOf(data.specializedSubject),
        //     classIndex: _self.data.classArr.indexOf(data.className)
        //   }) 
        // });   
      }
  },
  getSchooInfo:function(params,target){
    http.request({
      url: "/api/getDicDepartment",
      data: params,
      success: function (data) {
        if (data.length == 0) {
          _self.setData({
            [target]: []
          })
        }
        if (data.length > 0) {
                let dataArr = [];
                  data.forEach(function (item, index) {
                    if (target =='instituteArr'){
                      dataArr.push(item.college);
                   
                    }
                    if (target=='subjectArr'){
                      dataArr.push(item.major)
                    }
                    if (target == 'classArr') {
                      dataArr.push(item.dicClass)
                    }
                  });
          _self.setData({
            [target]: dataArr
          })

          if (target == 'instituteArr') {
            let updata = {
              year: _self.data.dateofEnrollment.substr(0, 4),
              college: _self.data.instituteArr[_self.data.instituteIndex]
            }
            _self.getSchooInfo(updata, "subjectArr");
          }
          if (target == 'subjectArr') {
            let updata = {
              year: _self.data.dateofEnrollment.substr(0, 4),
              college: _self.data.instituteArr[_self.data.instituteIndex],
              major: _self.data.subjectArr[_self.data.classIndex]
            }
            _self.getSchooInfo(updata, "classArr");
          }     
        }
        console.log(_self.data)
      }
    })
  },
  schoolNameChange:function(e){
    this.setData({
      schoolName: e.detail.value
    })
  },
  enrollDateChange:function(e){
    this.setData({
      dateofEnrollment:e.detail.value
    })
    //入学日期改变加载对应的学院
    let year = e.detail.value.substr(0,4);
    _self.getSchooInfo({ "year": year }, "instituteArr");
  },
  graduateDateChange:function(e){
    this.setData({
      dateofGraduation: e.detail.value
    })
  },
  collegeChange:function(e){
    console.log(e.detail.value);
    _self.setData(
      {
        instituteIndex: e.detail.value
      }
    )
    let update = {
      year:this.data.dateofEnrollment.substr(0,4),
      college:this.data.instituteArr[e.detail.value]
    }
    _self.getSchooInfo(update,"subjectArr");
  },
  subjectChange: function (e) {
    _self.setData(
      {
        subjectIndex: e.detail.value
      }
    )
    let update = {
      year: this.data.dateofEnrollment.substr(0, 4),
      college: this.data.instituteArr[this.data.instituteIndex],
      major:this.data.subjectArr[e.detail.value]
    }
    _self.getSchooInfo(update, "classArr");
  },
  formSubmit:function(e){
    if (!(this.data.instituteArr.length > 0 && this.data.instituteArr[this.data.instituteIndex])){
            Dialog.alert({
              message: "请选择学院,若无相关数据请联系管理员。"
            });
            return;
    }
    if (!(this.data.subjectArr.length > 0 && this.data.subjectArr[this.data.subjectIndex])) {
      Dialog.alert({
        message: "请选择专业,若无相关数据请联系管理员。"
      });
      return;
    }
    if (!(this.data.classArr.length > 0 && this.data.classArr[this.data.classIndex])) {
      Dialog.alert({
        message: "请选择班级,若无相关数据请联系管理员。"
      });
      return;
    }
    if (e.detail.value.schoolName==""){
      Dialog.alert({
        title: '提示',
        message: "校名不能为空。"
      });
      return;
    }

    let data = {
      "schoolName": e.detail.value.schoolName,
      "eucationalBackground": this.data.degreeArr[this.data.degreeIndex],
      "startDate": this.data.dateofEnrollment + "-01 00:00:00",
      "endDate": this.data.dateofGraduation +"-01 00:00:00",
      "college": this.data.instituteArr[this.data.instituteIndex],
      "specializedSubject": this.data.subjectArr[this.data.subjectIndex],
      "className": this.data.classArr[this.data.classIndex],
      "summary": e.detail.value.summary
    }
    if(this.data.type=="edit"){
      this.editEduExp(data);
    }else{
      this.addEduExp(data);
    }
   
  },
  addEduExp: function (eduData) {
    http.request({
      url: "/usr/addEducationalExperience",
      data: eduData,
      method: "POST",
      success: function (eduData) {

        Dialog.alert({
          message: '添加成功'
        }).then(() => {
          wx.navigateBack();
        });
    
      }
    })
  },
  editEduExp: function (eduData) {
    eduData.id = this.data.editData.id;
    http.request({
      url: "/usr/updateAddEducationalExperience",
      data: eduData,
      method: "POST",
      success: function (eduData) {
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          //关键在这里
          prePage.changeData();
          wx.navigateBack();
        }
      }
    })
  }
})
